using System;
using System.Collections.Generic;
using LoggingClient;

namespace DeviceManager.Common
{
    [Trace]
    public static class EventUtils
    {
        private static Logger euLogger = null;
        public static Logger EULogger
        {
            get
            {
                if (euLogger == null)
                {
                    euLogger = DMLogger.Instance;
                }

                return euLogger;
            }
        }

        public static void RaiseEvents(MulticastDelegate handler, params object [] args)
        {
            if (handler == null)
                return;

            Delegate [] invocationList = handler.GetInvocationList();

            foreach (Delegate invocation in invocationList)
            {
                try
                {
                    invocation.DynamicInvoke(args);
                }
                catch (Exception ex)
                {
                    EULogger.Error(string.Format("Exception caught when raising event ({0}) for client ({1}.{2}): {3}, InnerException: {4}",
                        handler.Method.Name, invocation.Target, invocation.Method.Name, ex.Message, ex.InnerException.Message));
                }
            }
        }
    }
}
